﻿using AdslMonitorLibrary;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Configuration;
using System.Threading;

namespace AdslMonitorlibrary
{
    public static class AdslMonitor
    {
        const string CATEGORY = "Roberto's monitoring";
        const string SNRCOUNTER = "Modem - SNR";

        public static bool SetupCategory()
        {
            if (PerformanceCounterCategory.Exists(CATEGORY))
                return false;

            CounterCreationDataCollection counterDataCollection = new CounterCreationDataCollection();

            // Add the counter.
            CounterCreationData snrCounter = new CounterCreationData();
            snrCounter.CounterType = PerformanceCounterType.NumberOfItems32;
            snrCounter.CounterName = SNRCOUNTER;
            counterDataCollection.Add(snrCounter);

            // Create the category.
            PerformanceCounterCategory.Create(CATEGORY,
                "All counters created by Roberto",
                PerformanceCounterCategoryType.SingleInstance, counterDataCollection);

            return true;
        }

        public static void Log()
        {
            AdslSnrReader reader = new AdslSnrReader(ConfigurationManager.AppSettings["modemAddress"], ConfigurationManager.AppSettings["userName"], ConfigurationManager.AppSettings["password"]);
            string fileName = ConfigurationManager.AppSettings["logFile"];
            string archieve = fileName + ".old";
            int maxSize = int.Parse(ConfigurationManager.AppSettings["MaxSizeMb"]) * 1024 * 1024;
            PerformanceCounter snrCounter = new PerformanceCounter(CATEGORY, SNRCOUNTER, false);
            //while (true)
            //{
                if (File.Exists(fileName) && (new FileInfo(ConfigurationManager.AppSettings["logFile"])).Length > maxSize)
                {
                    if (File.Exists(archieve))
                        File.Delete(archieve);
                    File.Copy(fileName, archieve);
                    File.Delete(fileName);
                }

                string snrdb = reader.ReadSnr();
                snrCounter.RawValue = Convert.ToInt32(double.Parse(snrdb) * 10.0);

                using (FileStream fs = File.Open(ConfigurationManager.AppSettings["logFile"], FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
                {
                    AddText(fs, CreateLogLine(snrdb));
                    fs.Close();
                }
                //Console.Write(CreateLogLine(reader.ReadSnr()));
              //  Thread.Sleep(int.Parse(ConfigurationManager.AppSettings["interval"]));
            //}
        }

        private static string CreateLogLine(string value)
        {
            return String.Format("{0:dd/MM/yyyy HH:mm:ss}\t{1}\r\n", DateTime.Now, value);
        }

        private static void AddText(FileStream fs, string value)
        {
            byte[] info = new UTF8Encoding(true).GetBytes(value);
            fs.Write(info, 0, info.Length);
        }

        public static void ClearCounter()
        {
            PerformanceCounter snrCounter = new PerformanceCounter(CATEGORY, SNRCOUNTER, false);
            snrCounter.RawValue = 0;
        }

    }
}
